﻿/* 功能:  商户结算管理
 * 创建人：Kencery  创建时间：2017-12-10
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.settlement = (function () {
    var defaults = {
        listUrl: "/Settlement/List",
        updateStatusUrl: "/Settlement/UpdateSettlementStatus",
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '结算ID',
        field: 'Id',
    }, {
        title: '商户名',
        field: 'Name',
        align: 'center',
    }, {
        title: '结算号',
        field: 'Number',
        align: 'center',
    }, {
        title: '订单总金额',
        field: 'OrderAmount',
        align: 'center',
    }, {
        title: '可结算金额',
        field: 'CanSettlementAmount',
        align: 'center',
    }, {
        title: '实际结算金额',
        field: 'ActSettlementAmount',
        align: 'center',
    }, {
        title: '创建结算人',
        field: 'Creater',
        align: 'center',
    }, {
        title: '创建时间',
        field: 'CreateTime',
        align: 'center',
    }, {
        title: '结算状态',
        field: 'StatusName',
        align: 'center',
    }, {
        title: '操作人',
        field: 'Optioner',
        align: 'center',
    }, {
        title: '操作时间',
        field: 'SettlementTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#settlementinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    }

    var queryParams = function (params) {
        return {
            offset: params.offset,             //后台计算显示数据信息
            pagesize: params.limit,            //每页显示多少行
            merchantname_search: $("#merchantname_search").val(),
            number_search: $("#number_search").val(),
            settlementstatus_search: $("#settlementstatus_search").val()
        };
    };

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });                                //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                                        //清空文本框信息

        $("#btnSettlementStatusComplete").on("click", function () { btnSettlementStatusComplete() });   //申请体现弹出浮层
        $("#btnSettlementStatusOK").on("click", function () { btnSettlementStatusOK() });               //申请体现

        $("#btnSettlementStatusFailure").on("click", function () { btnSettlementStatusFailure() });     //申请体现弹出浮层
        $("#btnSettlementStatusCancel").on("click", function () { btnSettlementStatusCancel() });       //申请体现弹出浮层
        $("#btnSettlementStatusRevokeOK").on("click", function () { btnSettlementStatusRevokeOK(); });  //申请体现
    };

    var btnQueryList = function () {
        plscommon.refreshTable("settlementinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnSettlementStatusComplete = function () {
        //结算单为已提交能操作，否则不能操作
        var data = plscommon.dialogWaringDisable('settlementinfo', 'SettlementStatusSuccessOperation');
        if (!data) {
            return false;
        }
        if (data.SettlementStatus != 1) {
            plscommon.warningMessage("该结算单已经处理过了，不允许在处理", 3000);
            return false;
        }
        $("#SettlementStatusSuccessModal").modal();

        opt.Type = 2;
        $("#success_id").val(data.Id);
        $("#Success_Head_Title_OK").text(data.Name);
    }

    var btnSettlementStatusOK = function () {
        var success_id = $("#success_id").val();
        if (!success_id) {
            plscommon.warningMessage("请您选择需要操作的数据", 4000);
            return false;
        }

        $("#btnSettlementStatusOK").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnSettlementStatusOK",
            url: defaults.updateStatusUrl,
            type: "POST",
            data: { Id: success_id, Type: opt.Type, Remark: "" },
            success: function () {
                if (this.status_code === 200) {
                    $('#SettlementStatusSuccessModal').modal('hide');
                    plscommon.refreshTable("settlementinfo");
                } else {
                    plscommon.warningMessage(this.status_message, 3000);
                }
            }
        });
    }

    var btnSettlementStatusFailure = function () {
        //结算单为已提交能操作，否则不能操作
        var data = plscommon.dialogWaringDisable('settlementinfo', 'SettlementStatusRevokeOperation');
        if (!data) {
            return false;
        }
        if (data.SettlementStatus != 1) {
            plscommon.warningMessage("该结算单已经处理过了，不允许在处理", 3000);
            return false;
        }
        $("#SettlementStatusRevokeModal").modal();

        opt.Type = 3;
        $("#settle_id").val(data.Id);
        $("#HeadTitle_Revoke").text("结算失败信息")
        $("#Title_Revoke").text("失败");
        $("#Remark").text("");
    }

    var btnSettlementStatusCancel = function () {
        //结算单为已提交能操作，否则不能操作
        var data = plscommon.dialogWaringDisable('settlementinfo', 'SettlementStatusRevokeOperation');
        if (!data) {
            return false;
        }
        if (data.SettlementStatus != 1) {
            plscommon.warningMessage("该结算单已经处理过了，不允许在处理", 3000);
            return false;
        }
        $("#SettlementStatusRevokeModal").modal();

        opt.Type = 4;
        $("#settle_id").val(data.Id);
        $("#HeadTitle_Revoke").text("结算取消信息");
        $("#Title_Revoke").text("取消");
        $("#Remark").text("");
    }

    var btnSettlementStatusRevokeOK = function () {
        var settle_id = $("#settle_id").val();
        if (!settle_id) {
            plscommon.warningMessage("请您选择需要操作的数据", 4000);
            return false;
        }
        var remark = $("#Remark").val();

        $("#btnSettlementStatusRevokeOK").prop("disabled", true);
        plscommon.ajax({
            disableId: "btnSettlementStatusRevokeOK",
            url: defaults.updateStatusUrl,
            type: "POST",
            data: { Id: settle_id, Type: opt.Type, Remark: remark },
            success: function () {
                if (this.status_code === 200) {
                    $('#SettlementStatusRevokeModal').modal('hide');
                    plscommon.refreshTable("settlementinfo");
                } else {
                    plscommon.warningMessage(this.status_message, 3000);
                }
            }
        });
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
        }
    };
}());